package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 林道样
 * @date 2020-02-24 18:40
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    public List<Map<String, Object>> getOrderSettingByMonth(String month) {
        if (month != null && month.length() > 0 && !"".equals(month)) {
            Map<String, String> map = new HashMap<>();
            map.put("begin", month + "-01");
            map.put("end", month + "-31");
            List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
            List<Map<String, Object>> mapList = new ArrayList<>();
            for (OrderSetting orderSetting : list) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("date", orderSetting.getOrderDate().getDate());
                map1.put("number", orderSetting.getNumber());
                map1.put("reservations", orderSetting.getReservations());
                mapList.add(map1);
            }
            return mapList;
        }
        new RuntimeException();
        return null;
    }

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        if (orderSetting == null) {
            new RuntimeException();
        }
        int number = orderSetting.getNumber();
        Date orderDate = orderSetting.getOrderDate();
        //判断是添加还是修改数据
        addOrUpdateOrderSetting(number, orderDate);
    }

    @Override
    public void upload(List<String[]> list) throws ParseException {
        if (list != null && list.size() > 0) {
            for (String[] strings : list) {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
                    Date orderDate = format.parse(strings[0]);
                    int number = Integer.parseInt(strings[1]);
                    //判断是添加还是修改数据
                    addOrUpdateOrderSetting(number, orderDate);
            }
        }

    }

    public void addOrUpdateOrderSetting(int number, Date date) {
        //判断当前日期是否设置过,如果设置过,修改预约人数
        OrderSetting orderSetting = orderSettingDao.findOrderSetting(date);
        if (orderSetting != null) {
            //判断当前日期预约人数是否大于设置人数
            int reservations = orderSetting.getReservations();
            if (number <= reservations) {
                return;
            }
            orderSetting.setNumber(number);
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            //如果没有设置过,添加设置信息
            OrderSetting orderSetting1 = new OrderSetting();
            orderSetting1.setOrderDate(date);
            orderSetting1.setNumber(number);
            orderSettingDao.addOrderSetting(orderSetting1);
        }
    }
}
